package com.aqie.meet.ui.search;

import android.util.Log;
import android.view.View;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.aqie.libcommon.adapter.CommonAdapter;
import com.aqie.libcommon.adapter.CommonViewHolder;
import com.aqie.libcommon.app.BaseFragment;
import com.aqie.libcommon.widget.PortraitView;
import com.aqie.libfactory.data.helper.MessageHelper;
import com.aqie.libfactory.model.db.Message;
import com.aqie.libfactory.model.db.Session;
import com.aqie.libfactory.model.db.User;
import com.aqie.meet.R;
import com.aqie.meet.ui.chat.MessageActivity;
import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 搜索聊天
 * A simple {@link Fragment} subclass.
 * create an instance of this fragment.
 */
public class SearchChatFragment extends BaseFragment implements SearchActivity.SearchFragment{
    @BindView(R.id.recycler_view)
    RecyclerView mRecycleView;

    private List<Message> mList = new ArrayList<>();
    private CommonAdapter<Message> mAdapter;

    @Override
    protected int getContentLayoutId() {
        return R.layout.fragment_search_chat;
    }

    @Override
    protected void initWidget(View root) {
        super.initWidget(root);
        initView();
    }

    private void initView() {
        mRecycleView.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRecycleView.addItemDecoration(new DividerItemDecoration(getActivity(),
                DividerItemDecoration.VERTICAL));
        mAdapter = new CommonAdapter<>(mList, new CommonAdapter.OnBindDataListener<Message>() {
            @Override
            public void onBindViewHolder(Message model, CommonViewHolder viewHolder, int type, int position) {
                User sender = model.getSender();
                sender.load();
                viewHolder.setText(R.id.txt_content, model.getMsg());
                viewHolder.setText(R.id.txt_name, sender.getUsername());
                PortraitView mPortrait = viewHolder.getView(R.id.im_portrait);
                mPortrait.setup(Glide.with(SearchChatFragment.this), sender);

                viewHolder.getView(R.id.chat).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        MessageActivity.show(getContext(), new Session(model));
                    }
                });
            }

            @Override
            public int getLayoutId(int type) {
                return R.layout.cell_chat_list;
            }
        });
        mRecycleView.setAdapter(mAdapter);
    }

    @Override
    protected void initData() {
        super.initData();
    }

    @Override
    public void search(String content) {
        getChatList(content);
    }

    /**
     * 获取搜索聊天列表
     */
    private void getChatList(String msg) {
        List<Message> list = MessageHelper.findLocalMessage(msg);
        if(list.size() == 0) {
            if(mRecycleView != null){
                mRecycleView.setVisibility(View.GONE);
            }
        } else {
            mRecycleView.setVisibility(View.VISIBLE);
            if (mList.size() > 0) {
                mList.clear();
            }
            mList.addAll(list);
            mAdapter.notifyDataSetChanged();
            Log.e(TAG, "message list:" + mList.toString());
        }
    }
}